<?php

    if ( !defined ( 'BASEPATH' ) )
        exit ( 'No direct script access allowed' );

    /*
     * Main confirmation checkout class
     * 
     */

    /*
     * -- Test za ovi klasu se nalazi na sledecim adresama za:
     * 
     * rentacar: http://localhost/globtour.me/en/checkout?trans_id=lAZk5zhAHVhPJZbsHrVupnVgJIg%3D
     * izlete: http://localhost/globtour.me/en/checkout?trans_id=O%2FPWLlEhZ%2BuiAmuYNjI2dmnSNKk%3D
     * hotele: http://localhost/globtour.me/en/checkout?trans_id=is6gZc4dbTUOQa5B0Tmw9zDd7H4%3D
     * 
     * 
     * -- Uputstvo za upotrebu
     * 
     * 
     * Polje result u tabeli transakcije su setovane na "result: OK"
     * U modelu invoicem na liniji 265 komentarisati: SEND INFO 1 TO AGENCY i SEND INFO 2 TO AGENCY
     * Kliknuti na svaku adresu posebno da bi se primio mejl
     * 
     */

    class Checkout extends CI_Controller {

        var $data;

        public function __construct ()
        {

            parent::__construct ();

            $this->load->library ( 'rest', array (
                'server' => REST_URL_RENTACAR
            ) );

            $this->data['action_footer_1'] = $this->rest->get ( 'action1', NULL, 'json' );
            $this->data['action_footer_2'] = $this->rest->get ( 'action2', NULL, 'json' );

            $this->rest->initialize ( array (
                'server' => REST_URL_EXCURSIONS
            ) );

            $this->data['hosting'] = HOST_URL_EXCURSIONS;
            $this->load->model ( 'cms_pro', 'cms' );
            $this->load->library ( 'template' );
            $this->load->library ( 'session' );
            $this->load->helper ( 'clean_html' );
            $this->load->helper ( 'language' );
            $this->load->helper ( 'urlify' );
            $this->load->helper ( 'gallery' );
            $this->load->helper ( 'week_days' );
            $this->load->helper ( 'inflector' );


            // load language file
            $this->lang->load ( 'excursions' );
            $this->lang->load ( 'rentacar' );
            $this->lang->load ( 'globtour' );
            $this->lang->load ( 'common' );

            require_once("urlify.php");
            $this->load->library ( 'translate', array ('lang' => $this->lang->lang ()) );
            $this->template->set_view ( "excursions" );
        }

        public function results ()
        {
            // redirect if transaction_ID is not set
            if ( !isset ( $_GET['trans_id'] ) ):
                redirect ( base_url () );
            //return;
            endif;

            $trans_id = $_GET['trans_id'];

            /* Hotels */
            //$trans_id = '491OaDrDL1wUfx5j8HzOAV/9cQM=';

            $params = array (
                'trans_id' => $trans_id
            );

            $transaction = $this->rest->post ( 'show_transaction', $params, 'json' );

//            $this->firephp->log ( ($transaction ) );
            $this->data['transaction'] = $transaction;

//            $transaction->result = 'OK';

            switch ( $transaction->result )
            {
                case 'DECLINED':

                    $data['title'] = 'DECLINED';
                    //echo 'Load result declined view';
                    $this->_proces_result ( $transaction, TRUE );
                    break;

                case 'FAILED':

                    $data['title'] = 'FAILED';
                    //echo 'Load result failed view';
                    $this->_proces_result ( $transaction, TRUE );
                    break;

                case 'OK':
                    $this->_proces_result ( $transaction );
                    break;

                default :
                    $data['title'] = 'DECLINED';
                    //echo 'Load result declined view';
                    $this->_proces_result ( $transaction, TRUE );
            }
        }

        private function _proces_result ( $transaction, $declined = FALSE )
        {
            //$this->firephp->log($transaction);

            switch ( $transaction->booking_type )
            {
                case "excursion":
                    $transaction->booking_type = "excursionbooking";
                    $status = 1;
                    break;
                case "tour":
                    $transaction->booking_type = "tourbooking";
                    $status = 1;
                    break;
                case "rentacar":
                    $transaction->booking_type = "carbooking";
                    $status = 0;
                    break;
                case "hotels":
                    $transaction->booking_type = "reservations";
                    $status = "CONFIRMED";
                    break;
            }// parse booking type

            $params = array (
                'trans_id' => $_GET['trans_id'],
                'type' => $transaction->booking_type
            );

            $booking_table = $this->rest->post ( 'show_booking_id', $params, 'json' );
//            $this->firephp->log($params);
//            $this->firephp->log ( $booking_table );

            $this->data['booking_info'] = $booking_table;

            // Update booking status
            if ( $transaction->booking_type === "reservations" )
            {
                $params = array (
                    'ID' => $booking_table->ID,
                    'type' => $transaction->booking_type,
                    'status' => $status,
                    'ENTER_PAYMENT' => TRUE,
                    'AMOUNT' => $transaction->amount / 100
                );
                $ID = $booking_table->ID;
            }
            else
            {
                $params = array (
                    'id' => $booking_table->id,
                    'type' => $transaction->booking_type,
                    'status' => $status
                );
                $ID = $booking_table->id;
            }

            // Set reservation ststus, Send bill to customers email
            if ( !$declined ):

                $res = $this->rest->post ( 'update_status', $params, 'json' );

                $this->_send_bill (
                        $transaction->booking_type, $ID
                );
            endif;


            // View checkout
            $this->_view_checkout ( $transaction->booking_type, $ID );
        }

        private function _view_checkout ( $booking_type, $orderID )
        {
            switch ( $booking_type )
            {
                case "excursionbooking":

                    $this->rest->initialize ( array (
                        'server' => REST_URL_EXCURSIONS
                    ) );

                    $this->data['title'] = "Booking Information";

                    $params = array (
                        'fbid' => $orderID,
                        'lang' => 'en'
                    );

                    $this->data['order'] = $this->rest->get ( 'total', $params, 'json' );
                    $this->data['show_paid'] = TRUE;

                    //$this->firephp->log($this->data);

                    $this->template->load ( 'excursions/checkout_3', $this->data );
                    break;
                case "tourbooking":

                    $this->rest->initialize ( array (
                        'server' => REST_URL_EXCURSIONS
                    ) );

                    $this->data['title'] = "Booking Information";

                    $params = array (
                        'fbid' => $orderID,
                        'lang' => 'en'
                    );

                    $this->data['order'] = $this->rest->get ( 'total_tour', $params, 'json' );
                    $this->data['show_paid'] = TRUE;

                    $this->template->load ( 'excursions/checkout_3', $this->data );
                    break;
                case "carbooking":

                    $this->rest->initialize ( array (
                        'server' => REST_URL_RENTACAR
                    ) );

                    $post = array (
                        'carbooking_id' => $orderID,
                        'lang' => $this->lang->lang ()
                    );

                    $response = $this->rest->post ( 'show_order', $post, 'json' );

                    //$this->firephp->log($this->rest->debug());

                    $this->data['order'] = $response[0][0];

                    $post = array (
                        'id' => $this->data['order']->carid,
                        'lang' => $this->lang->lang ()
                    );

                    //$this->firephp->log($post);
                    $car_response = $this->rest->post ( 'get_car', $post, 'json' );
                    $this->data['car'] = $car_response[0];

                    $this->load->library ( 'rentacar/booking_error' );
                    $order_extras = $this->booking_error->response_check ( $response[1] );

                    //$this->firephp->log($order_extras);

                    if ( $order_extras )
                    {
                        // If extras selected by post, get extras info from service and create html
                        /* $post = array( 
                          'id'=>$this->data['order']->carid
                          );

                          $response = $this->rest->post('accessories', $post, 'json'); */
                        $html = '';
                        foreach ( $order_extras as $val )
                        {

                            $data = array (
                                'data_id' => $val->adId,
                                'desc' => $val->type,
                                'text' => $val->price . ' &euro; / day'
                            );
                            $html.= $this->load->view ( 'rentacar/parts/checkout_list_item_extras', $data, TRUE );
                        }

                        $this->data['extras_html'] = $html;
                    }

                    //$this->firephp->log($this->data);

                    $this->data['method'] = 'checkout';
                    $this->data['show_paid'] = TRUE;
                    $this->template->load ( 'rentacar/checkout_3', $this->data );


                    break;

                case "reservations":

                    $this->rest->initialize ( array (
                        'server' => REST_URL_HOTELS,
                        'http_user' => 'cipa',
                        'http_pass' => 'booking',
                        'http_auth' => 'basic' // or 'digest' 
                    ) );

                    $this->data['title'] = "Booking Information";

                    $params = array (
                        'reservation_id' => $orderID,
                        'lang' => $this->lang->lang ()
                    );

                    $this->data['order'] = $this->rest->post ( 'showReservationInfo', $params );
                    $this->data['show_paid'] = TRUE;

                    //$this->firephp->log ( $this->data );

                    $this->template->load ( 'hotels/checkout_3', $this->data );


                    break;
            }
        }

        private function _send_bill ( $booking_type, $orderID )
        {
            //Send email to client  
            $this->load->model ( 'invoicem', 'invoicem' );
            $this->invoicem->prepare_and_send ( $booking_type, $orderID );
        }

    }

?>